package com.wontube.audio.pub;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * @author tanggf(2012-9-18)
 *
 */
public class AudioCookieUtils {
	private final static String LOCALE_COOKIE_KEY = "_audio_locale_";
	private final static int LOCALE_EPIRES = 30 * 24 * 3600;
	
	public static void setLocaleCookie(HttpServletResponse response, String locale){
		Cookie cookie = new Cookie(LOCALE_COOKIE_KEY, locale);
		cookie.setMaxAge(LOCALE_EPIRES);
		cookie.setPath("/");
		response.addCookie(cookie);
	}
	
	public static String getLocaleName(HttpServletRequest request){
		return getCookie(request, LOCALE_COOKIE_KEY);
	}
	
	/**
	 * @param request
	 * @param key
	 * @return
	 */
	public static String getCookie(HttpServletRequest request, String key){ 
		 String	value = null; 
		 Cookie[] cookies = request.getCookies();
		 if(cookies != null){ 
			 for (Cookie cookie : cookies) { 
				 if(key.equals(cookie.getName())){
					 value = cookie.getValue(); 
					 break; 
				 }
			 }
		 }
		  
		return value; 
	}
}
